/*
 *  Copyright (C) 2011 JiangHongTiao <jjurco.sk_gmail.com>
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package sk.lieskove301.jianghongtiao.math.statistic.distributions;

/**
 * Date of create: May 15, 2011
 *
 * @author JiangHongTiao <jjurco.sk_gmail.com>
 * @version 2011.0515
 */
public class NegativeBinomialDistribution implements Distribution {

    public NegativeBinomialDistribution() {
    }

    private int n = 1;
    private double p = 0.5;

    private cern.jet.random.NegativeBinomial distr =
            new cern.jet.random.NegativeBinomial(n, p,
            new cern.jet.random.engine.MersenneTwister(new java.util.Date()));

    public String getName() {
        return "Negative Binomial Distribution";
    }

    public String getLetter() {
        return "n";
    }

    /**
     *
     * @param double x (required)
     * @param int n - the number of trials (optional, but required with next parameter, if not present used default=0)
     * @param double p - the probability of success. (optional, if not present used default=1)
     * @return 1.0 - Math.exp(-1.0*Math.pow(x/alpha, beta));
     */
    public double cdf(double... attr) throws IllegalArgumentException {
        if((attr == null) || (attr.length == 0) || (attr.length > 3)){
            throw new IllegalArgumentException("invalid number of arguments");
        }
        distr.setNandP(n,p);
        if(attr.length > 2){
            distr.setNandP((int)attr[1], attr[2]);
        }
        return distr.cdf((int)attr[0]);
    }


    public double randomNumber() {
        return distr.nextDouble();
    }

    public double randomCdf() {
        return cdf(randomNumber());
    }

}
